package com.fbee.smartcommunities.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbee.smartcommunities.entity.ParkingSpace;
import com.fbee.smartcommunities.mapper.ParkingSpaceMapper;
import com.fbee.smartcommunities.service.ParkingSpaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbee.smartcommunities.service.UserService;
import com.fbee.smartcommunities.utils.LocalDateTimeUtil;
import com.fbee.smartcommunities.vo.ParkingSpaceVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-21
 */
@Service
public class ParkingSpaceServiceImpl extends ServiceImpl<ParkingSpaceMapper, ParkingSpace> implements ParkingSpaceService {

    @Resource
    private ParkingSpaceMapper parkingSpaceMapper;

    @Resource
    private UserService userService;

    @Override
    public Page<ParkingSpace> getPage(Integer pageNo, Integer pageSize, String keyword, String part) {
        if(keyword != null) keyword = keyword.trim();
        Page<ParkingSpace> parkingSpacePage = new Page<>(pageNo, pageSize);
        QueryWrapper<ParkingSpace> parkingSpaceWrapper = new QueryWrapper<>();
        if(keyword != null){
            parkingSpaceWrapper.like("car_number",keyword).or().eq("space_number",keyword);
        }
        return parkingSpaceMapper.getPageWithUserName(parkingSpacePage,keyword,part);
    }

    @Override
    public Boolean updateParkingSpace(Long id, Integer state, Long uid, Long begin, Long end, Double price, HttpServletRequest request) {
        // state 1为租赁 2为购买
        UpdateWrapper<ParkingSpace> updateWrapper = new UpdateWrapper<>();
        if(begin != null){
            LocalDateTime startTime = LocalDateTimeUtil.valueToLocalDateTime(begin);
            updateWrapper.set("start_time",startTime);
        }
        if(end != null){
            LocalDateTime endTime = LocalDateTimeUtil.valueToLocalDateTime(end);
            updateWrapper.set("end_time",endTime);
            }
        // 在这里进行扣费
        if(price > 0){
            String content;
            if( state == 1)
                content = "租赁车位：";
            else
                content = "购买车位：";
            userService.transfer(uid,new Long(50),price,request,content);
        }
        updateWrapper.set("state",state).set("user_id",uid).eq("id",id);
        return this.update(updateWrapper);
    }

    @Override
    public ParkingSpaceVO getMyParkingSpace(Long uid) {
        List<ParkingSpace> myRentedSlots = this.list(new QueryWrapper<ParkingSpace>().eq("user_id", uid).eq("state", 1));
        List<ParkingSpace> myPurchasedSlots = this.list(new QueryWrapper<ParkingSpace>().eq("user_id", uid).eq("state", 2));
        return new ParkingSpaceVO(myPurchasedSlots,myRentedSlots);
    }


}
